//
// Created by hugan on 2020/12/5.
//

#include "../dllJna/JoystickJna.cpp"
#include "../common/CommonUtil.h"

using namespace std;

void testJoystickJna() {
    static int stateReceiveCount;
    func_deviceStateCallback deviceStateCallback = [](DIJOYSTATE2 state, HRESULT hr) -> void {
        cout << "deviceStateCallback=" << stateReceiveCount << endl;
        CommonUtil::FAILED2("deviceState", hr);//如果有错误,才打印错误日志
        CommonUtil::printDeviceState(&state, false);
        if (stateReceiveCount++ > 300) stopListenState();//收到n次消息后就退出测试
    };
    if (CommonUtil::FAILED2("createDirectInput8", createDirectInput8(false, deviceStateCallback)))
        return;

    static int devIndex = 0;
    static string guidString;
    enumDevices([](EnumDeviceData data) -> void {
        devIndex++;
        cout << "dev." << devIndex << "=" << data.tszInstanceName << endl;
        cout << "  guid=" << data.guid << endl;
        //取最后一个设备
        guidString = data.guid;
    });

    //启动异步线程ExitInputThread
    CommonUtil::startExitInputThread([]() -> void {
        cout << "stopListenState" << endl;
        stopListenState();
    });

    //阻塞当前线程,监听设备:
    int fps[] = {90, 60, 30, 1};
    int stateScanSpan = 1000 / fps[1];
    int stickRange = 2000;
    bool breakIfErr = false;
    startListenState(guidString.c_str(), stateScanSpan, stickRange, breakIfErr);
    doFree();
    cout << "listen.out" << endl;
}

int main() {
    cout << "st" << endl;
    cout << "version=" << version() << endl;
    testJoystickJna();
    cout << "ed" << endl;
    return 0;
}